package Hangman;

import java.rmi.RemoteException;
import javax.ejb.*;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;

/**
 *
 * @author stuartsealfon
 */
@Stateless
public class HangmanBean implements HangmanBeanRemote {
    static final int NUMBER_OF_PLAYERS = 4;
    static ArrayList<Player> players = new ArrayList<Player>(NUMBER_OF_PLAYERS);
    static int currentPlayerCounter = 0;
    static boolean newGame = false;
    public static int turnCounter = 0;
    public UserBean turnTakingPlayer;
    public static ArrayList<String> usersLoggedIn = new ArrayList<String>();
    //EntityBean entity;

    //@PersistenceContext(unitName="EntityBean")
    //EntityManager u1;
    /*protected Users user;
    protected Collection<Users> userCollection;*/

    public boolean login(String userID, String pwd) {
        UsersFacadeRemote user = new UsersFacade();
        Users userInfo = user.find(userID);
        //System.out.println("***" + userInfo.getPassword() + "***");
        if (userInfo == null) return false;
        if (userInfo.getPassword().equals(pwd)) {
            if (!usersLoggedIn.contains(userID)) usersLoggedIn.add(userID);
            return true;
        }
        return false;
    }

    public boolean register(String userID, String pwd, String pwd2) {
        if (!pwd.equals(pwd2)) return false;
        UsersFacadeRemote user = new UsersFacade();
        if (user.find(userID) != null) return false;
        user.register(userID, pwd);
        System.out.println("Registered");
        return true;
    }

    public boolean remove(String userID) {
        UsersFacadeRemote user = new UsersFacade();
        Users userInfo = user.find(userID);
        if (user.find(userID) == null) return false;
        if (usersLoggedIn.contains(userID)) logout(userID);
        if (userInfo != null) user.remove(userID);
        return true;
    }

    public void logout(String userID) {
        for (int i=0; i<usersLoggedIn.size(); i++) {
            if (usersLoggedIn.get(i).equals(userID)) {
                usersLoggedIn.remove(i);
                break;
            }
        }
    }

    public String seeUsersInside() {
        String usersInside = "The Users logged in are: <br>";
        for (int i=0; i<usersLoggedIn.size(); i++) {
            if (usersLoggedIn.get(i) != null) usersInside += i+1 + ") " + usersLoggedIn.get(i);
            if (i < usersLoggedIn.size() - 1) usersInside += ",\n";
        }
        return usersInside;
    }

    public int addPlayerToGame(String username, String w) {
        int playerNumber = -1;
        Word word = new Word(w);
        if (!word.initializeWord()) return -1;
        Player player = new Player();
        player.setUserID(username, word);
        boolean inGame = false;
        for (int i=0; i<players.size(); i++) {
            if (players.get(i).username.equals(username)) {
                playerNumber = i;
                inGame = true;
            }
        }
        if (!inGame && players.size() < 4) {
            players.add(player);
            playerNumber = players.size();
        }
        else if (!inGame) return -1;
        return playerNumber;
    }

    public String getPlayerName(int i) {
        String name = null;
        boolean loggedIn = false;
        if (i < players.size()) {
            for (int j=0; j<usersLoggedIn.size(); j++) {
                name = players.get(i).toString();
                if (name.equals(usersLoggedIn.get(j))) {
                    loggedIn = true;
                    return name;
                }
            }
            if (!loggedIn && !isEliminated(i)) {
                players.remove(i);
                return null;
            }
            else if (!loggedIn) return name;
        }
        return null;
    }

    public boolean gameFull() {
        if (players.size() == 4) return true;
        else return false;
    }

    public char getLetter(int i) {
        int player = i/8;
        if (player >= players.size()) return '_';
        Player currentPlayer = players.get(player);
        int position = i%8;
        if (position >= currentPlayer.getWord().length()) return '_';
        return currentPlayer.word.getLetter(position);
    }

    public void guessLetter(char ch) {
        for (int i=0; i<8*4; i++) {
            int player = i/8;
            players.get(player).word.guessWord(ch);
        }
    }

    public boolean isEliminated(int playerNumber) {
        for (int i=0; i<players.get(playerNumber).word.letters.length; i++) {
            boolean loggedIn = false;
            for (int j=0; j<usersLoggedIn.size(); j++)
                if (players.get(playerNumber).toString().equals(usersLoggedIn.get(j))) loggedIn = true;
            if (!loggedIn) return true;
            if (players.get(playerNumber).word.letters[i].isGuessed == false) return false;
        }
        return true;
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 
}
